<?php
/**
 * Created by JetBrains NetBeans.
 * User: yinyancai
 * Date: 13-3-14 下午4:44
 * To change this template use File | Settings | File Templates.
 */
if (!defined('IN_MOBILE_API')) {
    exit('Access Denied');
}
include_once 'forum.php';
class cloud_api {
    function common() { }

    function output() {
        global $_G;
        if (empty($_GET['ccid']) || empty($_GET['order'])) {
            $variable = array(
                'code' => 1001,
                'messagestr' => lang('plugin/cloud', '1001'),
            );
            cloud_core::result(cloud_core::variable($variable));
        }
        if (!in_array($_GET['order'], array('rebate', 'viewnum', 'meanPrice', 'marketnum'))) {
            $variable = array(
                'code' => 0,
                'messagestr' => lang('plugin/cloud', '1018'),
            );
            cloud_core::result(cloud_core::variable($variable));
        }
        //页码数
        $page = max(1, intval($_GET['page']));
        //每页显示大小
        $max = empty($_GET['maxsize']) ? 10 : intval($_GET['maxsize']);
        //得到新的页面偏移起始位置
        $start = ($page - 1) * $max;

        $data = C::t('#cloud#cloud_product')->fetch_all_by_category_product($_GET['ccid'], $_GET['order']);
        foreach ($data as $key => $value) {
            if (!empty($value['picurl'])) {
                $data[$key]['picurl'] = $_G['siteurl'] . 'data/attachment/product/' . $value['picurl'];
            }
            $standard = C::t('#cloud#cloud_product_standard')->fetch_first_by_product_id($value['cpid']);
            $data[$key]['standards'] = $standard;
        }
		
		if($_GET['order'] == 'meanPrice'){
			$datas = $data;
			foreach ($datas as $k=>$val) {
				$n_arr[$k] = $val['standards'][0]['price'];
			}
				asort($n_arr);
			foreach ($n_arr as $k=>$val) {
				$data_array[$k] = $datas[$k];
			}
			//正常返回
			$variable = array(
				'data' =>array_values($data_array),
				'maxsize' => $max,
				'page' => $page,
			);
			cloud_core::result(cloud_core::variable($variable));
		}else{
			//正常返回
			$variable = array(
				'data' => cloud_core::getvalues($data, array('/^\d+$/')),
				'maxsize' => $max,
				'page' => $page,
			);
			cloud_core::result(cloud_core::variable($variable));
		}
		
    }
}

?>

